package com.wwp.tbshop.view.adapter;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.support.v13.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwp.tbshop.R;
import com.wwp.tbshop.view.fragment.CategoryFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 楠GG on 2017/6/15.
 */

public class CategoryPagerAdapter extends FragmentPagerAdapter {
    private Context mContext;
    private CategoryFragment mCategoryFragment;

    private List<Fragment> mFragmentList = new ArrayList<>();

    private String[] mTitles = {"妈妈专区", "时尚女装", "宝宝用品", "日常用品", "儿童服饰", "儿童玩具", "箱包",
            "手机数码", "内衣", "家电", "食品", "家装", "家居家纺", "图书乐器", "游戏动漫"};

    private int[] mNormalImages = {R.drawable.category_mom_selector,
            R.drawable.category_fashion_selector, R.drawable.category_childuses_selector,
            R.drawable.category_baihuo_selector, R.drawable.category_muying_selector,
            R.drawable.category_childtoy_selector, R.drawable.category_box_selector,
            R.drawable.category_camera_selector,R.drawable.category_neiyi_selector,
            R.drawable.category_jiadian_selector, R.drawable.category_food_selector,
            R.drawable.category_jiazhuang_selector, R.drawable.category_jiaju_selector,
            R.drawable.category_book_selector, R.drawable.category_game_selector
    };

    public void setFragmentList(List<Fragment> fragmentList) {
        mFragmentList = fragmentList;
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        super.finishUpdate(container);
//        if(mCategoryFragment.isFirstLoad) {
            mCategoryFragment.mOnPageChangeListener.onPageSelected(0);
//            mCategoryFragment.isFirstLoad = false;
//        }
    }

    public CategoryPagerAdapter(FragmentManager fm, Context context, CategoryFragment categoryFragment) {
        super(fm);
        mContext = context;
        mCategoryFragment = categoryFragment;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }

    public View getTabView(int position){
        View view = LayoutInflater.from(mContext).inflate(R.layout.icon_layout, null);
        TextView textView = (TextView) view.findViewById(R.id.title_tv);
        ImageView img = (ImageView) view.findViewById(R.id.img);
        img.setImageResource(mNormalImages[position]);
        textView.setText(mTitles[position]);
        return view;
    }

}
